<template>
  <el-dialog model-value="dialogVisible" :title="dialogTitle" width="60%" @close="handleClose">
    <div style="max-height: 520px;overflow: auto;">
      <el-form :inline="true" ref="formRef" :model="form" :rules="rules" label-width="100px">

        <el-form-item label="样木ID" prop="tree" style="width: 250px;">
          <el-input v-model="form.tree" disabled />
        </el-form-item>
        <el-form-item label="部位" prop="part" style="width: 250px;">
          <el-input v-model="form.part" disabled />
        </el-form-item>
        <br>
        <el-form-item label="0根颈处" prop="p0">
          <el-input-number v-model="form.p0" :min="0" :precision="1" :step="0.1" controls-position="right">
            <template #suffix> <span>cm</span> </template>
          </el-input-number>
        </el-form-item>
        <el-form-item label="0.5/10处" prop="p05">
          <el-input-number v-model="form.p05" :min="0" :precision="1" :step="0.1" controls-position="right">
            <template #suffix> <span>cm</span> </template>
          </el-input-number>
        </el-form-item>
        <el-form-item label="1/10处" prop="p1">
          <el-input-number v-model="form.p1" :min="0" :precision="1" :step="0.1" controls-position="right">
            <template #suffix> <span>cm</span> </template>
          </el-input-number>
        </el-form-item>
        <el-form-item label="2/10处" prop="p2">
          <el-input-number v-model="form.p2" :min="0" :precision="1" :step="0.1" controls-position="right">
            <template #suffix> <span>cm</span> </template>
          </el-input-number>
        </el-form-item>
        <el-form-item label="3/10处" prop="p3">
          <el-input-number v-model="form.p3" :min="0" :precision="1" :step="0.1" controls-position="right">
            <template #suffix> <span>cm</span> </template>
          </el-input-number>
        </el-form-item>
        <el-form-item label="4/10处" prop="p4">
          <el-input-number v-model="form.p4" :min="0" :precision="1" :step="0.1" controls-position="right">
            <template #suffix> <span>cm</span> </template>
          </el-input-number>
        </el-form-item>
        <el-form-item label="5/10处" prop="p5">
          <el-input-number v-model="form.p5" :min="0" :precision="1" :step="0.1" controls-position="right">
            <template #suffix> <span>cm</span> </template>
          </el-input-number>
        </el-form-item>
        <el-form-item label="6/10处" prop="p6">
          <el-input-number v-model="form.p6" :min="0" :precision="1" :step="0.1" controls-position="right">
            <template #suffix> <span>cm</span> </template>
          </el-input-number>
        </el-form-item>
        <el-form-item label="7/10处" prop="p7">
          <el-input-number v-model="form.p7" :min="0" :precision="1" :step="0.1" controls-position="right">
            <template #suffix> <span>cm</span> </template>
          </el-input-number>
        </el-form-item>
        <el-form-item label="8/10处" prop="p8">
          <el-input-number v-model="form.p8" :min="0" :precision="1" :step="0.1" controls-position="right">
            <template #suffix> <span>cm</span> </template>
          </el-input-number>
        </el-form-item>
        <el-form-item label="9/10处" prop="p9">
          <el-input-number v-model="form.p9" :min="0" :precision="1" :step="0.1" controls-position="right">
            <template #suffix> <span>cm</span> </template>
          </el-input-number>
        </el-form-item>

        <el-form-item label="材积" prop="volume">
          <el-input-number v-model="form.volume" :min="0" :precision="2" :step="0.1" controls-position="right">
            <template #suffix> <span>m³</span> </template>
          </el-input-number>
        </el-form-item>

        <el-form-item label="备注" prop="remark" style="width: 250px;">
          <el-input v-model="form.remark" type="textarea" :rows="1" />
        </el-form-item>

      </el-form>
    </div>

    <template #footer>
      <span class="dialog-footer">
        <el-button type="primary" @click="handleConfirm">确认</el-button>
        <el-button @click="handleClose">取消</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { defineEmits, defineProps, ref, watch } from "vue";
import api, { getServerUrl } from "@/util/request";
import { ElMessage } from 'element-plus'

const props = defineProps(
  {
    id: {
      type: Number,
      default: -1,
      required: true
    },
    dialogTitle: {
      type: String,
      default: '',
      required: true
    },
    dialogVisible: {
      type: Boolean,
      default: false,
      required: true
    }
  }
)

const form = ref({
  id: null,
  tree: 0,
  part: '',
  p0: null,
  p05: null,
  p1: null,
  p2: null,
  p3: null,
  p4: null,
  p5: null,
  p6: null,
  p7: null,
  p8: null,
  p9: null,
  remark: '',
})


const rules = ref({
  address: [
    { required: true, message: '请选择样地！' },
  ],
  TH: [
    { type: 'number', message: '必须为数字', trigger: 'blur' },
  ]
})

const formRef = ref(null)

const initFormData = async (id) => {
  const res = await api.get("treedia/action?id=" + id);
  form.value = res.data.data;
}

watch(
  () => props.dialogVisible,
  () => {
    let id = props.id;
    if (id != -1) {
      initFormData(id)
    } else {
      form.value = {
        id: -1,
        address: 0,
        year: new Date(),
        treeSpecies: 0,
        number: null,
        TH: null,
        DBH: null,
        DC: null,
        volume: null,
        HFLB: null,
        CW_SN: null,
        CW_EW: null,
        remark: '',
      }
    }

  }
)


const emits = defineEmits(['update:modelValue', 'initTreeDiaList'])

const handleClose = () => {
  emits('update:modelValue', false)
}

const handleConfirm = () => {
  formRef.value.validate(async (valid) => {
    if (valid) {
      let result = await api.post("treedia/save", form.value);
      let data = result.data;
      if (data.code == 200) {
        ElMessage.success(data.msg)
        formRef.value.resetFields();
        emits("initTreeDiaList")
        handleClose();
      } else {
        ElMessage.error(data.msg);
      }
    } else {
      console.log("字段校验失败！")
    }
  })
}

</script>

<style scoped></style>
